<?php
/**
 * Created by openXtiger.org.
 * User: xtiger
 * Date: 2009-6-20
 * Time: 11:43:44
 */
function cms_releasedmodel_select($siteid,$catid,$page,$pageline,$field='',$q='') {
    $dbmodel = xt_load('model','databasemodel');
    $likekeys = array();
    $intkeys = array();
    $strkeys = array();
    if($siteid>0){
        $intkeys = array('t2.siteid'=>$siteid);   
    }
    if($catid>0) {
         $intkeys['t2.catid'] = $catid;
    }
    switch($field) {
        case 'subject':
            $likekeys['t2.subject'] = $q;break;
        case 'userid':
            $intkeys['t2.userid'] = $q;break;
        case 'itemid':
            $intkeys['t2.contentid'] = $q;break;
        case 'username':
             $strkeys['t2.username'] = $q;break;
            
    }

	$randkeys = array('dateline'=>array('sstrtotime','t2.dateline'), 'updatetime'=>array('sstrtotime','t2.updatetime'));

	$results = $dbmodel('getwheres',$intkeys, $strkeys, $randkeys, $likekeys);

	$wherearr = $results['wherearr'];
	$wheresql = empty($wherearr)?'1':implode(' AND ', $wherearr);
    $mpurl = count($results['urls'])>0?'&'.implode('&', $results['urls']):'';

	$orders = $dbmodel('getorders',array('dateline'=>'t2.dateline', 'updatetime'=>'t2.updatetime'), '');

	$ordersql = $orders['sql'];
	if($orders['urls']) $mpurl .= '&'.implode('&', $orders['urls']);
	$r = $dbmodel('multipage',"SELECT t2.* FROM ".xt_tname('content','icweb','released')." t1.contentid=t2.contentid WHERE $wheresql", $ordersql, $page, $pageline,'icweb');
	$r['pageurl'] = $mpurl;
	return $r;
}


function cms_releasedmodel_get($contentid) {
    $contentid = intval($contentid);
    $db = xt_load('db','icweb');
    $data = $db->fetch_first("SELECT * FROM `".xt_tname('content','icweb')."` WHERE `contentid`=$contentid");
    return $data;
}

function cms_releasedmodel_getmodel($contentid,$model_table) {
    $contentid = intval($contentid);
    $db = xt_load('db','icweb');
    $data = $db->fetch_first("SELECT * FROM `".xt_tname($model_table,'icweb')."` WHERE `contentid`=$contentid");
    return $data;
}
function cms_releasedmodel_add($data) {
    global $_UD;
    $db = xt_load('db','icweb');
    $cm = xt_load('model','cms.contentmodel');
    $c = $cm('get',$data['contentid']);
    if(!$c) {
        return -2;
    }

    $data['subject'] = $c['subject'];
    $data['userid'] = $_UD['id'];
    $data['username'] = $_UD['userid'];
    $data['status'] = 0;
    if($db->result($db->query("SELECT count(*) from `".xt_tname('released','icweb')."` WHERE `siteid`=$data[siteid] AND `contentid`=$c[contentid]"))>0){
        return $db->update('released',$data,"`siteid`='$data[siteid]' AND `contentid`=$c[contentid]",'icweb');
    }
    $data['catid'] = $c['catid'];
    $r = $db->insert('released',$data,0,'icweb');
    if($r>0) {
        return $db->query("UPDATE `".xt_tname('content','icweb')."` SET released=(select count(*) from `".xt_tname('released','icweb')."` WHERE `contentid`=$c[contentid]) WHERE `contentid`=$c[contentid]");
    }
}
function cms_releasedmodel_edit($contentid, $data, $modelid, $tablename){
    $db = xt_load('db','icweb');
    require_once MS_FIELDS_PATH.'content_input.class.php';
    //require_once CACHE_MODEL_PATH.'content_update.class.php';
    $content_input = new content_input($modelid);
    $inputinfo = $content_input->get($data);
    if(!is_array($inputinfo)) {
        return $inputinfo;
    }
    $systeminfo = $inputinfo['system'];
    $modelinfo = $inputinfo['model'];
    if(!$systeminfo['username']) $systeminfo['username'] = 'xtiger';
    if(!$systeminfo['userid']) $systeminfo['userid'] = '1';
    $r = $db->update('content', $systeminfo,"`contentid` = $contentid",'icweb');
    if($modelinfo) $db->update($tablename, $modelinfo,"`contentid` = $contentid",'icweb');
    return TRUE;
}
?>